package com.haizhi.workstation.exception;

import com.haizhi.workstation.result.CodeMsg;
import com.haizhi.workstation.result.Result;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * @ControllerAdvice 对Controller的增强 标签
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler({BindException.class, GlobalException.class})
    @ResponseBody
    public Result<CodeMsg> handleException(Exception e) {
        if (e instanceof GlobalException) {
            GlobalException bex = (GlobalException) e;
            return Result.error(((GlobalException) e).getCodeMsg());
        } else {
            return null;
        }
    }

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result<CodeMsg> exceptionHandler(Exception e) {
        e.printStackTrace();
        return Result.error(CodeMsg.ERR_OPERATION);
    }
}
